<?php
use app\common\config\autoreply_config;
use app\common\helper\appmsg_helper;
?>
{include file="../app/common/admin/lib/header.html" title="群发信息"}
{css href="./static/css/admin/multi_send.css"}
<div class="service">
  <div class="page-header border-bottom clearfix">
    <h2 class="left">群发消息<span>在这里针对指定店铺一键群发</span></h2>
  </div>
  <div class="service-box clearfix">
    <div class="service-mass">
      <form class="js_form" action="" method="post">

        <div class="s-mass-item s-mass-con">
          <div class="s-mass-options js_typeBox">
            <a href="javascript:void(0);" class="mass-btn js_textTab curr" data-type="1"><em class="mass-word"></em>文字</a>
            <!--{/*<a href="javascript:void(0);" class="mass-btn js_appmediaTab" data-type="10"><em class="mass-pic"></em>图片</a>-->
            <!--<a href="javascript:void(0);" class="mass-btn js_appmediaTab" data-type="20"><em class="mass-music"></em>音频</a>-->
            <!--<a href="javascript:void(0);" class="mass-btn js_appmediaTab" data-type="30"><em class="mass-video"></em>视频</a>*/}-->
            <a href="javascript:void(0);" class="mass-btn js_appmsgTab" data-type="40" data-only-sync="1" data-store-id="-1"><em class="mass-word-pic"></em>单图文</a>
            <a href="javascript:void(0);" class="mass-btn js_appmsgTab" data-type="50" data-only-sync="1" data-store-id="-1"><em class="mass-multi-word-pic"></em>多图文</a>
          </div>
          <div class="js_contentBox">
            <div class="js_contentTab1">
              <div contenteditable="true" class="s-mass-txt js_messageContent"></div>
              <div class="s-mass-options clearfix">
                <a href="javascript:void(0);" class="mass-btn js_faceBtn"><em class="mass-face"></em></a>
                <!--{*<p class="s-mass-txt-count right">还可输入600字</p>*}-->
              </div>
            </div>
            <div class="s-mass-txt js_contentTab10" style="height:200px; display:none;"></div>
            <div class="s-mass-txt js_contentTab20" style="height:200px; display:none;"></div>
            <div class="s-mass-txt js_contentTab30" style="height:300px; display:none;"></div>
            <div class="s-mass-txt js_contentTab40" style="height:300px; display:none;"></div>
            <div class="s-mass-txt js_contentTab50" style="height:300px; display:none;"></div>
          </div>
        </div>
        <button type="submit" class="btn btn-success btn-sm js_okBtn">发送</button>
      </form>
    </div>
  </div>
</div>
<div class="js_formBox">

</div>
<div class="js_frameBox">
</div>
<style>
#weiboface { left:215px !important; /*top:440px !important;*/ }
#weiboface #weiboface-face-arrow { display:block !important; }
</style>
{js href='./static/js/module/appmedia.js'}
<script>
var allUserNum = 0;
var sendSuccessNum = 0;
var sendFailNum = 0;
var sendTimes = 0;
var perNum = 2;
var interVal;
var userIds;
var multiSendId = 0;

$(function(){
  // 切换分区与店铺
  $(":input[name='send_type']").change(function(e){
    var currValue = $(this).val();
    
    // 按店铺
    if (currValue == 1) {
      $(".js_preStoreSelectWrap").show();
      $(".js_preAreaSelectWrap").hide();
      
    } else if (currValue == 2) {
      $(".js_preStoreSelectWrap").hide();
      $(".js_preAreaSelectWrap").show();
    }
  });
  
  // =============== 选择店铺与取消选择店铺 （或大区）======================
  // 选择店铺，点击了全选
  $('.js_preCusSelectAll, .js_preCusSelectAllTxt').click(function(e){
    var parentObj = $(this).closest('.js_preCusSelectWrap');
    var checkBox  = false;
    if ($(this).hasClass('js_preCusSelectAllTxt')) {
      if ($(this).prev().attr('checked') == 'checked') {
        $(this).prev().attr('checked', false);
      } else {
        $(this).prev().attr('checked', true);
        checkBox  = true;
      }
    }
    if ($(this).attr('checked') == 'checked') {
      checkBox  = true;
    }
    parentObj.find('.js_preCusSelect').attr('checked', checkBox);
  });
  
  // 选择了的某个店铺
  $('.js_preCusSelect, .js_preCusSelectTxt').click(function(){
    var parentObj = $(this).closest('.js_preCusSelectWrap');
    var checkBox  = true;
    if ($(this).hasClass('js_preCusSelectTxt')) {
      if ($(this).prev().attr('checked') == 'checked') {
        $(this).prev().attr('checked', false);
        checkBox  = false;
      } else {
        $(this).prev().attr('checked', true);
      }
    }
    parentObj.find('.js_preCusSelect').each(function(){
      if ($(this).attr('checked') != 'checked') {
        checkBox  = false;
      };
    });
    //alert(checkBox);
    //console.log(parentObj.find('.js_sltAll'), checkBox);
    parentObj.find('.js_preCusSelectAll').attr('checked', checkBox);
  });
  //=============== 选择店铺与取消选择店铺 ======================
  
  
  // 表情
  weiboFace($(".js_faceBtn"), $('.js_messageContent'));
  
  // 发送的消息类型切换
  $(".js_typeBox a").click(function(e){
    e.preventDefault();
    
    $(this).siblings('a').removeClass('curr').end().addClass('curr');
    var type = $(this).attr('data-type');
    
    $(".js_contentTab"+type).show().siblings('div').hide();
  });
  
  // 执行群发任务
  $(".js_okBtn").click(function(e){
    e.preventDefault();
    
    var thisObj = $(this);
    
    var targetType = 0;
    var allStore = 0;
    var storeIds = [];
    var areaIds  = [];
    
    // 如果是按店铺筛选
    if ($('.js_preStoreSelectWrap:visible').length >= 1) {
      if ($('.js_preStoreSelectWrap .js_preCusSelectAll:checked').length >= 1) {
        allStore = 1;
      }
      
      $('.js_preStoreSelectWrap .js_preCusSelect:checked').each(function(){
        storeIds.push($(this).val());
      });
      if (storeIds.length < 1) {
        showMsg('请选择要发送的店铺，至少要选一个');
        return false;
      }
      
      targetType = 1;
      
    } else {
      if ($('.js_preAreaSelectWrap .js_preCusSelectAll:checked').length >= 1) {
        allStore = 1;
      }
      
      $('.js_preAreaSelectWrap .js_preCusSelect:checked').each(function(){
        areaIds.push($(this).val());
      });
      if (areaIds.length < 1) {
        showMsg('请选择要发送的分区，至少要选一个');
        return false;
      }
      
      targetType = 2;
    }
    
    var type     = $(".js_typeBox a.curr").attr('data-type');
    var content  = '';
    if (type == 1) {
      content = $(".js_messageContent").html();
    } else {
      content = $(".js_contentTab"+type).attr('data-content');
    }
    
    if (!type) {
      showMsg('消息类型不能为空');
      return false;
    }
    
    if (!content) {
      showMsg('群发内容不能为空');
      return false;
    }
    
    if (thisObj.attr('is-post') == 1) {
      return false;
    }
    thisObj.attr('is-post', 1);
    showConfirm('你确定要执行此群发操作吗', 1, function(){
      var postData = { 'all_store':allStore, 'target_type':targetType, 'area_id':areaIds.join(','), 'store_id':storeIds.join(','), 'type':type, 'content':content };
      //console.log(postData);return;
      $.post(siteUrl+'/send/admin/save', postData, function(json){
        if (json.info != 'ok') {
          thisObj.attr('is-post', 0);
          showMsg(json.info);
          return false;
        }
        
        showMsg('消息已入群发队列，执行需要一定的时间，请耐心等待', 1);
        
        $('.js_textTab').trigger('click');
        $(".js_messageContent").html('');
        
        setTimeout(function(){
          thisObj.attr('is-post', 0);
          //location.reload(); 
        }, 1000)
      }, 'json').error(function(){
        thisObj.attr('is-post', 0);
      });
    }, function(){
      thisObj.attr('is-post', 0);
    });
    
      
    // 发送时显示遮罩
    //showLoading('正在群发，请稍候');
  });


  // 发送至手机预览
  $('.js_preview').click(function(e){
    e.preventDefault();
    var userType = $("select[name='user_type']").val();
    var tagId    = $("select[name='tag_id']").val();
    var type     = $(".js_typeBox a.curr").attr('data-type');
    var user_id  = $("input[name='user_id']").val();
    var content  = '';

    if (type == 1) {
      content = $(".js_messageContent").html();
    } else {
      content = $(".js_contentTab"+type).attr('data-content');
    }

    if(!user_id){
      showMsg('User_id不能为空');
      return false;
    }

    if (!type) {
      showMsg('消息类型不能为空');
      return false;
    }

    if (!content) {
      showMsg('群发内容不能为空');
      return false;
    }

    var postData = { 'tag_id':tagId, 'user_type':userType, 'type':type, 'user_id':user_id, 'content':content };

    $.post(siteUrl+'/message/admin/multi_send/preview', postData, function(json){
      if (json.info != 'ok') {
        alert('失败');
        return ;
      }

      alert('成功');
    }, 'json');
  });
  
});

function realSend(userId, type)
{
  $.post(siteUrl+'/message/admin/multi_send/do_send', { 'to_user_id':userId, 'type':type, 'multi_send_id':multiSendId }, function(json){
    if (json.info != 'ok') {
      sendFailNum++;
      
      consoleLog(json.info);
      return false;
    }
    
    sendSuccessNum++;
  }, 'json').error(function(){
    sendFailNum++;
  });
}

function loapSend(type)
{
  for (var i=0;i<perNum;i++)
  {
    consoleLog(i+sendTimes*perNum);//0,1
    if (typeof userIds[i+sendTimes*perNum] != 'undefined') {
      realSend(userIds[i+sendTimes*perNum], type);
    }
  }
}
</script>
{include file="../app/common/admin/lib/footer.html"}